<template>
  <d2-container>
    <div class="mod-tenant-monitor">
      <!-- 顶部筛选区域 -->
      <div class="filter-section">
        <div class="filter-left">
          <span class="filter-label">所付费项目显示</span>
        </div>
        <div class="filter-right">
          <el-select v-model="selectedBuilding" placeholder="房间拓扑" size="small" style="width: 120px;">
            <el-option label="房间拓扑" value=""></el-option>
          </el-select>
          <el-select v-model="selectedStatus" placeholder="仪表状态" size="small" style="width: 120px; margin-left: 10px;">
            <el-option label="未开户" value="未开户"></el-option>
            <el-option label="欠费" value="欠费"></el-option>
            <el-option label="报警" value="报警"></el-option>
            <el-option label="正常" value="正常"></el-option>
            <el-option label="异常" value="异常"></el-option>
          </el-select>
        </div>
      </div>
      <!-- 租户监控卡片列表 -->
      <div class="monitor-cards">
        <div v-for="building in buildingList" :key="building.name" class="building-section">
          <div class="building-header">
            <i class="el-icon-office-building"></i>
            <span>{{ building.name }}</span>
          </div>
          <div class="tenant-cards">
            <div v-for="tenant in building.tenants" :key="tenant.id" class="tenant-card">
              <div class="tenant-header">
                <div class="tenant-avatar">
                  <i class="el-icon-user"></i>
                </div>
                <div class="tenant-info">
                  <div class="tenant-name">{{ tenant.userName }}</div>
                  <div class="tenant-details">
                    <span>预付金额: {{ tenant.prepaidAmount }}</span>
                    <span class="status-badge" :class="getStatusClass(tenant.alarmLevel)">{{ tenant.alarmLevel }}</span>
                  </div>
                  <div class="tenant-usage">
                    <span>今日用电: {{ tenant.todayUsage }}</span>
                    <span class="status-badge" :class="getStatusClass(tenant.status)">{{ tenant.status }}</span>
                  </div>
                  <div class="tenant-water">
                    <span>今日用水: {{ tenant.todayWater }}</span>
                    <span class="status-badge" :class="getStatusClass(tenant.status)">{{ tenant.status }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </d2-container>
</template>
<script>
export default {
  name: 'tenant-monitor',
  data() {
    return {
      selectedBuilding: '',
      selectedStatus: '',
      buildingList: [],
      dataListLoading: false
    }
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    async getDataList() {
      this.dataListLoading = true
      try {
        const data = await this.$api.tenant_monitor_list({
          page: 1,
          limit: 100
        })
        // 按楼栋分组数据
        const groupedData = {}
        data.data.page.list.forEach(item => {
          if (!groupedData[item.buildingName]) {
            groupedData[item.buildingName] = {
              name: item.buildingName,
              tenants: []
            }
          }
          groupedData[item.buildingName].tenants.push(item)
        })
        this.buildingList = Object.values(groupedData)
      } catch (error) {
        console.error('获取数据失败:', error)
        this.$message.error('获取数据失败')
      } finally {
        this.dataListLoading = false
      }
    },
    // 获取状态样式类
    getStatusClass(status) {
      const statusMap = {
        '报警': 'status-alarm',
        '欠费': 'status-debt',
        '正常': 'status-normal',
        '异常': 'status-error',
        '在线': 'status-online',
        '合闸': 'status-close'
      }
      return statusMap[status] || 'status-default'
    }
  }
}
</script>
<style scoped>
.mod-tenant-monitor {
  padding: 20px;
  background: #f5f5f5;
  min-height: calc(100vh - 120px);
}
.filter-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.filter-left {
  display: flex;
  align-items: center;
}
.filter-label {
  color: #333;
  margin-right: 10px;
}
.filter-link {
  color: #409eff;
  padding: 0;
}
.filter-right {
  display: flex;
  align-items: center;
}
.monitor-cards {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.building-section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  overflow: hidden;
}
.building-header {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  background: #f8f9fa;
  border-bottom: 1px solid #e9ecef;
  font-weight: 500;
  color: #333;
}
.building-header i {
  margin-right: 8px;
  color: #409eff;
}
.tenant-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 15px;
  padding: 20px;
}
.tenant-card {
  border: 1px solid #e9ecef;
  border-radius: 6px;
  padding: 15px;
  background: #fff;
  transition: all 0.3s ease;
}
.tenant-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  transform: translateY(-2px);
}
.tenant-header {
  display: flex;
  align-items: flex-start;
}
.tenant-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  flex-shrink: 0;
}
.tenant-avatar i {
  color: #fff;
  font-size: 18px;
}
.tenant-info {
  flex: 1;
}
.tenant-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 8px;
}
.tenant-details,
.tenant-usage,
.tenant-water {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  font-size: 14px;
  color: #666;
}
.status-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}
.status-alarm {
  background: #fee;
  color: #f56c6c;
}
.status-debt {
  background: #fef0e6;
  color: #e6a23c;
}
.status-normal {
  background: #f0f9ff;
  color: #67c23a;
}
.status-error {
  background: #fee;
  color: #f56c6c;
}
.status-online {
  background: #f0f9ff;
  color: #409eff;
}
.status-close {
  background: #f0f9ff;
  color: #409eff;
}
.status-default {
  background: #f5f5f5;
  color: #909399;
}
</style>
